﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Typing_GUI
{
    public partial class frmManageLesson_Admin : Form
    {
        public frmManageLesson_Admin()
        {
            InitializeComponent();
        }
        public int userId;
        void LoadAvatar_Level()
        {
            string un = CLASS.CNguoiDung.SetAvatar_Level(userId, pbAvatar, pbLevel);
            lblUsername.Text = un;
            ToolTip toolTip1 = new ToolTip();
            toolTip1.SetToolTip(pbLevel, pbLevel.Tag.ToString());
        }
        private void dgvLessonList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radText.Checked == true)
            {
                frmCreateTextLesson fm = new frmCreateTextLesson();
                fm.editorID = userId;
                fm.ShowDialog();
                LoadData();
            }
            else
            {
                frmCreateAdventureLesson fm = new frmCreateAdventureLesson();
                fm.userId = userId;
                fm.ShowDialog();
                LoadData();
            }
        }
        void LoadData()
        {
            DataTable dt = CLASS.CBaiTap.GetLessonListFull();

            dgvLessonList.DataSource = dt;
            dgvLessonList.Columns[0].HeaderText = "No";
            dgvLessonList.Columns[1].HeaderText = "Lesson Mame";
            dgvLessonList.Columns[2].HeaderText = "Content";
            dgvLessonList.Columns[3].HeaderText = "Descriptive";
            dgvLessonList.Columns[4].HeaderText = "Time";
            dgvLessonList.Columns[5].HeaderText = "Max score";
            dgvLessonList.Columns[6].HeaderText = "Level";
            dgvLessonList.Columns[7].HeaderText = "Type";
            dgvLessonList.Columns[8].HeaderText = "Send PeoPle";
            dgvLessonList.Columns[9].HeaderText = "Status";
            dgvLessonList.Columns[10].Visible = false;
            dgvLessonList.Columns[11].Visible = false;
            dgvLessonList.Columns[12].Visible = false;
            dgvLessonList.Columns[1].MinimumWidth = 50;
        }
        private void frmManageLesson_Admin_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadAvatar_Level();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<int> mang_ma = new List<int>();
            if (dgvLessonList.SelectedRows.Count > 0)
            {
                DialogResult kq = MessageBox.Show("Do you want to delete this lesson?", "Notices", MessageBoxButtons.OKCancel);
                if (kq != DialogResult.Cancel)
                {
                    for (int i = dgvLessonList.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        mang_ma.Add(int.Parse(dgvLessonList.SelectedRows[i].Cells[0].Value.ToString()));
                        CLASS.CBaiTap.DeleteLesson(int.Parse(dgvLessonList.SelectedRows[i].Cells[0].Value.ToString()));
                        dgvLessonList.Rows.Remove(dgvLessonList.SelectedRows[i]);

                    }

                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvLessonList.SelectedRows.Count > 0)
            {

                CLASS.CBaiTap bt = new Typing_GUI.CLASS.CBaiTap();
                bt.Mabaitap = int.Parse(dgvLessonList.SelectedRows[0].Cells[0].Value.ToString());
                bt.Tenbaitap = dgvLessonList.SelectedRows[0].Cells[1].Value.ToString();
                bt.Noidung = dgvLessonList.SelectedRows[0].Cells[2].Value.ToString();
                bt.Mota = dgvLessonList.SelectedRows[0].Cells[3].Value.ToString();
                bt.Thoigian = int.Parse(dgvLessonList.SelectedRows[0].Cells[4].Value.ToString());
                bt.Diemtoida = int.Parse(dgvLessonList.SelectedRows[0].Cells[5].Value.ToString());
                bt.Capdobaitap = int.Parse(dgvLessonList.SelectedRows[0].Cells[10].Value.ToString());
                bt.Loaibaitap = int.Parse(dgvLessonList.SelectedRows[0].Cells[11].Value.ToString());
                bt.Nguoisoan = int.Parse(dgvLessonList.SelectedRows[0].Cells[12].Value.ToString());
                bt.TinhTrang = bool.Parse(dgvLessonList.SelectedRows[0].Cells[9].Value.ToString());
                if (bt.Loaibaitap == 1)
                {
                    frmEditTextLesson fm = new frmEditTextLesson();
                    fm.editorID = bt.Nguoisoan;
                    fm.LessonID = bt.Mabaitap;
                    fm.adminUse = true;
                    //////////////////////////////////////////////////////////////////////////////                 
                    fm.ShowDialog();
                    if (fm.updated == true)
                    {
                        dgvLessonList.SelectedRows[0].Cells[3].Value = fm.lesson.Mota;
                        dgvLessonList.SelectedRows[0].Cells[1].Value = fm.lesson.Tenbaitap;
                        dgvLessonList.SelectedRows[0].Cells[2].Value = fm.lesson.Noidung;
                        dgvLessonList.SelectedRows[0].Cells[4].Value = fm.lesson.Thoigian.ToString();
                        dgvLessonList.SelectedRows[0].Cells[5].Value = fm.lesson.Diemtoida.ToString();
                        dgvLessonList.SelectedRows[0].Cells[8].Value = fm.username;
                        dgvLessonList.SelectedRows[0].Cells[6].Value = fm.levelLesson;
                        dgvLessonList.SelectedRows[0].Cells[10].Value = fm.lesson.Capdobaitap.ToString();
                        dgvLessonList.SelectedRows[0].Cells[11].Value = fm.lesson.Loaibaitap.ToString();
                        dgvLessonList.SelectedRows[0].Cells[12].Value = fm.lesson.Nguoisoan.ToString();
                        dgvLessonList.SelectedRows[0].Cells[9].Value = fm.lesson.TinhTrang.ToString();
                    }
                }
                else
                {
                    frmEditAdventureLesson fm = new frmEditAdventureLesson();
                    fm.bt = bt;
                    fm.UserId = userId;
                    fm.ShowDialog();
                    if (fm.updated == true)
                    {
                        dgvLessonList.SelectedRows[0].Cells[3].Value = fm.bt.Mota;
                        dgvLessonList.SelectedRows[0].Cells[1].Value = fm.bt.Tenbaitap;
                        dgvLessonList.SelectedRows[0].Cells[2].Value = fm.bt.Noidung;
                        dgvLessonList.SelectedRows[0].Cells[4].Value = fm.bt.Thoigian.ToString();
                        dgvLessonList.SelectedRows[0].Cells[5].Value = fm.bt.Diemtoida.ToString();
                        dgvLessonList.SelectedRows[0].Cells[10].Value = fm.bt.Capdobaitap.ToString();
                        dgvLessonList.SelectedRows[0].Cells[11].Value = fm.bt.Loaibaitap.ToString();
                        dgvLessonList.SelectedRows[0].Cells[12].Value = fm.bt.Nguoisoan.ToString();
                        dgvLessonList.SelectedRows[0].Cells[9].Value = fm.bt.TinhTrang.ToString();
                        dgvLessonList.SelectedRows[0].Cells[8].Value = fm.username;
                        dgvLessonList.SelectedRows[0].Cells[6].Value = fm.levelLesson;
                    }
                }
               
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dgvLessonList_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }

        private void dgvLessonList_DoubleClick(object sender, EventArgs e)
        {
            btnUpdate_Click(null, null);
        }
    }
}